﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Text;
using DotNet.Business;
using DotNet.Model;
using DotNet.Common;
using System.Threading;
using System.Collections.Generic;
using System.EnterpriseServices;

public partial class Admin_adminResourceClass : basePage
{
    private int selectIndex = 0;
    resourceClassBll rCbll = new resourceClassBll();
    ResourceBll rBll = new ResourceBll();
    private static readonly object objSync = new object();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["state"]!=null)
            {
                string state = Request.QueryString["state"].ToString();
                int resourceClassId =Convert.ToInt32(Request.QueryString["resourceClassId"].ToString());
                switch (state.Trim())
                {
                    case "update":
                        Update(resourceClassId);
                        break;
                    case "add":
                        Insert();
                        break;
                    case "edit":
                        Edit(resourceClassId);
                        break;
                    case "del":
                        Delete(resourceClassId);  
                        break;
                    default:
                   
                        break;
                }
            }
        }
        BindResourceClass();
    }   
    #region 编辑---郑同飞
    public void Edit(int resourceClassID)
    {
        resourceClass rCModel=new resourceClass();
        rCModel = rCbll.GetModel(resourceClassID);
        this.resourceClass_hid.Value = rCModel.ResourceClassID.ToString();
        this.txt_resourceClassName.Value = rCModel.ResourceClassName;
        selectIndex =Convert.ToInt32(rCModel.Type);
    }
    #endregion

    #region 资源类型下拉框
    public string typeDp(string id,int count) 
    {           
        string str = "<select id=\""+id+"\">";
        for (int i = 0; i < count; i++)
		{
            if (i == selectIndex)
            {
                str += "<option selected=\"selected\" id=\"" + id + "\" value=\"" + i + "\">" + i + "</option>";
            }
            else
                str += "<option value=\""+i+"\">"+i+"</option>";		    
		}
        return str;       
    }
    #endregion

    #region 绑定---郑同飞
    public void BindResourceClass() 
    {
        this.Repeater1.DataSource = rCbll.GetModel();
        this.Repeater1.DataBind();

    }
    #endregion

    #region 添加---郑同飞
    public void Insert() 
    {
        resourceClass rCModel = new resourceClass();
        rCModel.ResourceClassName = Request.Form["resourceClassName"].ToString();
        rCModel.Type = Request.Form["type"].ToString();
        if (rCbll.Insert(rCModel))
        {
 
        }
    }
    #endregion

    #region 更新---郑同飞
    public void Update(int resourceClassID) 
    {
        resourceClass rCModel = new resourceClass();
        rCModel.ResourceClassID = resourceClassID;
        rCModel.ResourceClassName = Request.Form["resourceClassName"].ToString();
        rCModel.Type = Request.Form["type"].ToString();
        if (rCbll.Update(rCModel))
        {

        }
    }
    #endregion

    #region 删除---郑同飞
    public void Delete(int resourceClassID) 
    {
        bool bo = false;       
        IList<Resource> rList = rBll.GetModel();
        foreach (Resource  r in rList)
        {
            if (bo)
            {
                break;
            }
            if (r.ResourceClassID==resourceClassID)
            {
                bo = true;
                break;
            }
        }
        if (!bo)
        {
            if (rCbll.Delete(resourceClassID))
            {

            }
        }
        else 
        {
            Response.Write("<font color='red'>资源分类已经被资源使用</font>");
        }
    }
    #endregion

    #region 获取该类别下的资源数量---郑同飞
    /// <summary>
    ///  获取该类别下的资源数量---郑同飞
    /// </summary>
    /// <param name="resourceClassID"></param>
    /// <returns></returns>
    public int countByClass(int resourceClassID)
    {
        return rBll.countByClass(resourceClassID);
    }
    #endregion



}
